import time

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

class Base():
    def __init__(self,driver):
        self.driver=driver

    def open_url(self,url):
        self.driver.get(url)

    #截屏
    def jieping(self):
        jp=time.strftime("%Y%m%d%H%M%S",time.localtime())
        self.driver.save_screenshot("../Png"+jp+".png")

    #定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed)
            return self.driver.find_element(*loc)

        except Exception as e:
            print(e)
            self.jieping()

    #点击
    def click(self,loc):
        self.find_element(loc).click()

    #输入内容
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    #文本
    def wenben(self,loc):
        return self.find_element(loc).text

    #回车
    def ENTER(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)

    #鼠标双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()

    #切换窗口
    def new_page(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    #Iframe
    def Iframe(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #下拉框
    def Select(self,loc,x):
        value=Select(self.find_element(loc))
        value.select_by_value(x)

    #弹窗处理(确定)
    def alert_accept(self):
        self.driver.switch_to.alert.accept()

    #清除事件
    def clear(self,loc):
        self.find_element(loc).clear()

    #滚动条
    def scroll(self):
        self.driver.execute_script.scrollTo("window scrollTo(0,0)")
